/* 
 *  This file is part of hmvcBlog-blog.
 *
 *  hmvcBlog-blog is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  hmvcBlog-blog is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with hmvcBlog-blog.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.frontend.bloglist.client.bloglist;

import java.util.Set;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.bloglist.client.bloglist.events.in.ShowBloglistViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.client.applicationframe.events.in.AddApplicationViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.historytoken.client.HistoryToken;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.i18n.client.I18N;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.BlogPreviewDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.in.GetSetBlogPreviewDTOEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.ReceiveSetBlogPreviewDTOEvent;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.registry.controller.AControllerModelViewRegistry;

/**
 * 
 * 
 * @author jens.meiss
 */
public class BloglistController extends
		AControllerModelViewRegistry<BloglistModel, BloglistView> {

	public void handleEvent(IEvent event) {
		
		if (event.getClass() == ShowBloglistViewEvent.class) {
			
			fireEventGlobal(new AddApplicationViewEvent(getView().getLabelTitle(), getView().getWidget()));
			
		} else if (event.getClass() == ReceiveSetBlogPreviewDTOEvent.class) {
			
			refreshBloglist(((ReceiveSetBlogPreviewDTOEvent) event).getSetBlogPreviewDTO());
			
		}
		
	}
	
	public void init() {
		
		register(ShowBloglistViewEvent.class);
		register(ReceiveSetBlogPreviewDTOEvent.class);
		
		fireEventGlobal(new GetSetBlogPreviewDTOEvent(this));
		
	}

	public void initView() {
		
		Label labelTitle = new Label(I18N.constants.labelBloglistTitle());
		getView().setLabelTitle(labelTitle);
		
		AbsolutePanel absolutePanelContainer = new AbsolutePanel();
		absolutePanelContainer.setStylePrimaryName("blogList");
		getView().setAbsolutePanelContainer(absolutePanelContainer);
		
		Hyperlink hyperlinkLogin = new Hyperlink(I18N.constants.linkLogin(), HistoryToken.login);
		getView().setHyperlinkLogin(hyperlinkLogin);
		
		Hyperlink hyperlinkRegister = new Hyperlink(I18N.constants.linkRegister(), HistoryToken.register);
		getView().setHyperlinkLogin(hyperlinkRegister);

		HorizontalPanel horizontalPanelLoginRegister = new HorizontalPanel();
		horizontalPanelLoginRegister.setSpacing(3);
		horizontalPanelLoginRegister.setStylePrimaryName("panelLoginRegister");
		horizontalPanelLoginRegister.add(hyperlinkLogin);
		horizontalPanelLoginRegister.add(hyperlinkRegister);
		getView().setHorizontalPanelLoginRegister(horizontalPanelLoginRegister);			
		
		Label labelHostedBlogs = new Label();
		labelHostedBlogs.setStylePrimaryName("labelHostedBlogs");
		getView().setLabelHostedBlogs(labelHostedBlogs);
		
		VerticalPanel verticalPanelBloglist = new VerticalPanel();
		getView().setVerticalPanelBloglist(verticalPanelBloglist);		
		
		/*
		 * build view
		 */
		absolutePanelContainer.add(horizontalPanelLoginRegister);
		absolutePanelContainer.add(labelHostedBlogs);
		absolutePanelContainer.add(verticalPanelBloglist);
		
	}

	private void refreshBloglist(Set<BlogPreviewDTO> setBlogPreviewDTO) {
		
		VerticalPanel panel = getView().getVerticalPanelBloglist();
		panel.clear();
		
		if ((setBlogPreviewDTO == null) || (setBlogPreviewDTO.size() == 0)) {
			
			getView().getLabelHostedBlogs().setText(I18N.constants.labelNoBlogsFound());
			
		} else {
			
			getView().getLabelHostedBlogs().setText(I18N.constants.labelHostedBlogs());
			
			for (BlogPreviewDTO blogPreviewDTO : setBlogPreviewDTO) {
				
				Hyperlink hyperlink = new Hyperlink(blogPreviewDTO.getTitle(), HistoryToken.blog + "=" + blogPreviewDTO.getId());
				Label labelAuthor = new Label(I18N.constants.labelAuthorName() + " " + blogPreviewDTO.getUsername());
				
				Label labelBlogDescription = new Label(blogPreviewDTO.getDescription());
				
				FlexTable flexTable = new FlexTable();
				flexTable.setStylePrimaryName("blogPreview");
				flexTable.setWidget(0, 0, hyperlink);
				flexTable.setWidget(0, 1, labelAuthor);
				flexTable.setWidget(1, 0, labelBlogDescription);
				
				FlexCellFormatter flexCellFormatter = flexTable.getFlexCellFormatter();
				flexCellFormatter.setColSpan(1, 0, 2);
				
				panel.add(flexTable);
				
			}
		}
		
	}

	public void terminate() {
	}

	public void initModel() {
	};

}
